package Day22.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @version 1.0
 * @BelongsProject:IntelliJ IDEA
 * @BelongsPackage:Day22.reflect
 * @Author:hyhWTX
 * @ClassName:ReflectDemo02
 * @CreateTime:2022年-10月-31日 17:00
 * @Description: 反射的使用2，获得构造方法
 */
public class ReflectDemo02 {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        getMethod();
//        Class cla = Person.class;
//        Object obj = cla.newInstance();
//        Method methodName = cla.getDeclaredMethod("setName", String.class);
//        Object value = methodName.invoke(obj, "张三！");
//        if (value==null){
//            System.out.println("返回值为空！");
//        }else {
//            System.out.println("返回返回值"+value);
//        }
//        //获得对象
//        Method methodAddress = cla.getDeclaredMethod("setAddress",String.class);
//        Object addressValue = methodAddress.invoke(obj,"中国");
//        if (addressValue==null){
//            System.out.println("返回值为空！");
//        }else {
//            System.out.println("返回返回值"+addressValue);
//        }

    }
    //获得参数列表
    public static void getMethod(){
        Class cla = Person.class;
        //获得类中的方法
        Method[] methods = cla.getDeclaredMethods();
        for (Method method : methods) {
            //获得方法名
            String name = method.getName();
            //获得方法返回值类型
            Class  returnType = method.getReturnType();
            //获得方法修饰符
            String methodModifiers = Modifier.toString(method.getModifiers());
            //获得参数列表
            Class[] methodParameterTypes = method.getParameterTypes();
            //获得方法参数抛出异常
            Class[] methodExceptionTypes = method.getExceptionTypes();
            System.out.println(methodModifiers+" "+returnType+" "+name);
            System.out.println("参数列表");
            for (Class methodParameterType : methodParameterTypes) {
                System.out.println(methodParameterType);
            }
            System.out.println("抛出的异常类型");
            for (Class methodExceptionType : methodExceptionTypes) {
                System.out.println(methodExceptionType);
            }
            System.out.println("=====================");
        }
    }
}
